/**
 * @param {string} S
 * @return {number}
 */

const uniqueLetterString = function (S: string) {

    let sum = 0

    for (let i = 0; i < S.length; i++) {

        const c = S[i]
        let j = i - 1
        for (; j >= 0 && S[j] != c; j--) {
        }
        let k = i + 1
        for (; k < S.length && S[k] != c; k++) {
        }

        // 将问题转换成 字母c 在解中出现的次数统计, 所有字母出现的次数总和就是
        sum += (i - j) * (k - i)
    }

    return sum

};


console.log(uniqueLetterString("ABC"));
